package dyy.zc.computergradeexam.utils;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import dyy.zc.computergradeexam.application.MyApplication;
import dyy.zc.computergradeexam.bean.BianChengInfo;
import dyy.zc.computergradeexam.bean.ChooseInfo;
import dyy.zc.computergradeexam.bean.Knowledge;
import dyy.zc.computergradeexam.bean.TianKong;
import dyy.zc.computergradeexam.bean.UserInfo;
import dyy.zc.computergradeexam.bean.WrongQuestion;
import dyy.zc.computergradeexam.constant.Constants;
import dyy.zc.computergradeexam.ui.activity.BianChengActivity;
import dyy.zc.computergradeexam.ui.activity.EditInfoActivity;
import dyy.zc.computergradeexam.ui.activity.LoginActivity;
import dyy.zc.computergradeexam.ui.activity.RegisterActivity;
import dyy.zc.computergradeexam.ui.activity.TianKongActivity;
import dyy.zc.computergradeexam.ui.activity.XuanZeActivity;


/**
 * Created by chuan on 2017/4/11.
 */

public class BmobUtils {
    private Context context;
    public static boolean loginSuccess = false;
    private boolean registerSuccess = true;

    public BmobUtils(Context context) {
        this.context = context;
    }

    /**
     * 登录查询是否账号密码输入错误
     */
    public void queryAccount(final UserInfo account) {
        BmobQuery<UserInfo> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<UserInfo>() {
            @Override
            public void done(List<UserInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("queryAccount", "ok");
                    for (UserInfo a :
                            list) {
                        if (a.getAccount().equals(account.getAccount()) && a.getPassword().equals(account.getPassword())) {
                            loginSuccess = true;
                            MyApplication.getInstance().saveUserInfo(a);
                            break;
                        }
                    }
                } else {
                    e.printStackTrace();
                    Log.e("queryAccount", "Failed");
                }
                ((LoginActivity) context).handler.sendEmptyMessage(Constants.ResultCode.RESULT_SUCCESS);
            }
        });
    }

    /**
     * 找回密码
     */
    public void getBackYourAccount(final String phone, final String userPassword) {
        final BmobQuery<UserInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<UserInfo>() {
            @Override
            public void done(List<UserInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("getBackYourAccount", "ok");
                    for (UserInfo a :
                            list) {
                        if (a.getAccount().equals(phone)) {
                            a.setPassword(userPassword);
                            findPassword(a);
                            break;
                        }
                    }
                } else {
                    Log.e("getBackYourAccount", e.getMessage());
                }
            }
        });
    }

    /**
     * 更新用户密码
     *
     * @param account
     */
    public void findPassword(UserInfo account) {
        account.update(account.getId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.e("updateAccountInfo", "ok");
                    ((RegisterActivity) context).handler.sendEmptyMessage(Constants.ResultCode.SUCCESS);
                } else {
                    e.printStackTrace();
                    Log.e("updateAccountInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 上传图片文件--注册时默认的用户头像
     *
     * @param bmobFile
     * @param account
     */
    public void upMoRenFile(final BmobFile bmobFile, final UserInfo account) {
        bmobFile.upload(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    account.setHead(bmobFile);
                    addUserInfo(account);
                    Log.e("upLoadFile", "ok--" + bmobFile.getFileUrl());
                } else {
                    e.printStackTrace();
                    Log.e("upLoadFile", e.getMessage());
                }
            }
        });
    }

    /**
     * 增加用户
     *
     * @param account
     */
    public void addUserInfo(final UserInfo account) {
        account.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    ((RegisterActivity) context).account.setId(s);
                    updateInfo(((RegisterActivity) context).account);
                    ((RegisterActivity) context).handler.sendEmptyMessage(Constants.ResultCode.SUCCESS);
                } else {
                    Log.e("Bmob-updateInfoFailed", e.getMessage());
                }
            }
        });
    }

    /**
     * 注册新用户验证
     *
     * @param userPhone 账号
     */
    public void registerChecked(final String userPhone) {
        BmobQuery<UserInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<UserInfo>() {
            @Override
            public void done(List<UserInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("list", list + "");
                    Log.e("registerChecked", "ok");
                    for (UserInfo a :
                            list) {
                        if (a.getAccount().equals(userPhone)) {
                            registerSuccess = false;
                            break;
                        }
                    }
                    if (!registerSuccess) {
                        ((RegisterActivity) context).handler.sendEmptyMessage(Constants.ResultCode.REGISTER_FAILED);
                    } else {
                        ((RegisterActivity) context).handler.sendEmptyMessage(Constants.ResultCode.REGISTER_SUCCESS);
                    }
                } else {
                    Log.e("registerChecked", e.getMessage());
                }

            }
        });
    }

    /**
     * 编辑个人信息页面需要用到的上传图片
     *
     * @param bmobFile
     * @param userInfo
     */
    public void EditInfoUpLoadFile(final BmobFile bmobFile, final UserInfo userInfo) {
        bmobFile.upload(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.e("EditInfoUpLoadFile", "ok");
                    userInfo.setHead(bmobFile);
                    MyApplication.getInstance().saveUserInfo(userInfo);
                    ((EditInfoActivity) context).handler.sendEmptyMessage(Constants.ResultCode.SUCCESS);
                } else {
                    Log.e("EditInfoUpLoadFile", e.getMessage());
                    ((EditInfoActivity) context).handler.sendEmptyMessage(Constants.ResultCode.FAILED);
                }
            }
        });
    }

    /**
     * 更新用户信息----编辑个人信息页面
     *
     * @param userInfo
     */
    public void EditInfpUpdateInfo(final UserInfo userInfo) {
        userInfo.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.e("EditInfpUpdateInfo", "ok");
                    userInfo.setNewInfo(true);
                    MyApplication.getInstance().saveUserInfo(userInfo);
                    updateInfo(userInfo);
                    ((EditInfoActivity) context).handler.sendEmptyMessage(Constants.ResultCode.RESULT_SUCCESS);
                } else {
                    Log.e("EditInfpUpdateInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 更新用户信息
     *
     * @param userInfo
     */
    public void updateInfo(UserInfo userInfo) {
        userInfo.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.e("updateInfo", "ok");
                } else {
                    Log.e("updateInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 获取选择题
     */
    public void getChooseInfo(final String account, final String type, final List<ChooseInfo> chooseInfos, final int resultCode, final Handler handler){
        BmobQuery<ChooseInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<ChooseInfo>() {
            @Override
            public void done(List<ChooseInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("getChooseInfo", "ok"+list.size());
                    chooseInfos.addAll(list);
                    getWrongQuestion(account,type,resultCode,handler);

                } else {
                    Log.e("getChooseInfo", e.getMessage());
                }
            }
        });
    }

    public void getChooseExam(final List<ChooseInfo> chooseInfos, final int resultCode, final Handler handler){
        BmobQuery<ChooseInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<ChooseInfo>() {
            @Override
            public void done(List<ChooseInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("getChooseInfo", "ok"+list.size());
                    chooseInfos.addAll(list);
                    handler.sendEmptyMessage(resultCode);
                } else {
                    Log.e("getChooseInfo", e.getMessage());
                }
            }
        });
    }
    public void getTianKongExam(final List<TianKong> chooseInfos, final int resultCode, final Handler handler){
        BmobQuery<TianKong> query = new BmobQuery<>();
        query.findObjects(new FindListener<TianKong>() {
            @Override
            public void done(List<TianKong> list, BmobException e) {
                if (e == null) {
                    Log.e("getChooseInfo", "ok"+list.size());
                    chooseInfos.addAll(list);
                    handler.sendEmptyMessage(resultCode);
                } else {
                    Log.e("getChooseInfo", e.getMessage());
                }
            }
        });
    }
    public void getBianExam(final List<BianChengInfo> chooseInfos, final int resultCode, final Handler handler){
        BmobQuery<BianChengInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<BianChengInfo>() {
            @Override
            public void done(List<BianChengInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("getChooseInfo", "ok"+list.size());
                    chooseInfos.addAll(list);
                    handler.sendEmptyMessage(resultCode);
                } else {
                    Log.e("getChooseInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 获取错题集
     * @param account
     * @param resultCode
     * @param handler
     */
    public void getWrongQuestion(final String account, final String type, final int resultCode, final Handler handler){
        BmobQuery<WrongQuestion> query = new BmobQuery<>();
        query.findObjects(new FindListener<WrongQuestion>() {
            @Override
            public void done(List<WrongQuestion> list, BmobException e) {
                if (e == null) {
                    Log.e("getWrongQuestion", "ok"+list.size());
                    for (WrongQuestion wrongQuestion:list){
                        if (wrongQuestion.getAccount().equals(account)&&wrongQuestion.getType().equals(type)){
                            switch (type){
                                case "1":((XuanZeActivity)context).wrongQuestion = wrongQuestion;break;
                                case "2":((TianKongActivity)context).wrongQuestion = wrongQuestion;break;
                                case "3":((BianChengActivity)context).wrongQuestion = wrongQuestion;break;
                            }
                            break;
                        }
                    }
                    handler.sendEmptyMessage(resultCode);
                } else {
                    Log.e("getWrongQuestion", e.getMessage());
                }
            }
        });
    }

    /**
     * 保存错题
     * @param wrongQuestion
     * @param resultCode
     * @param handler
     */
    public void saveWrongQuestion(WrongQuestion wrongQuestion,final int resultCode, final Handler handler){
        wrongQuestion.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e==null){
                    Log.e("saveWrongQuestion", "ok");
                    handler.sendEmptyMessage(resultCode);
                }else {
                    Log.e("saveWrongQuestion", e.getMessage());
                }
            }
        });
    }

    /**
     * 更新错题集
     * @param wrongQuestion
     * @param resultCode
     * @param handler
     */
    public void updateWrongQuestion(WrongQuestion wrongQuestion,final int resultCode, final Handler handler){
        wrongQuestion.update(wrongQuestion.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e==null){
                    Log.e("updateWrongQuestion", "ok");
                    handler.sendEmptyMessage(resultCode);
                }else {
                    Log.e("updateWrongQuestion", e.getMessage());
                }
            }
        });
    }

    /**
     * 获取填空题
     */
    public void getTianKongInfo(final String account, final String type, final List<TianKong> tianKongs, final int resultCode, final Handler handler){
        BmobQuery<TianKong> query = new BmobQuery<>();
        query.findObjects(new FindListener<TianKong>() {
            @Override
            public void done(List<TianKong> list, BmobException e) {
                if (e == null) {
                    Log.e("getTianKongInfo", "ok"+list.size());
                    tianKongs.addAll(list);
                    getWrongQuestion(account,type,resultCode,handler);
                } else {
                    Log.e("getTianKongInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 获取编程题
     */
    public void getBianChengInfo(final String account, final String type, final List<BianChengInfo> tianKongs, final int resultCode, final Handler handler){
        BmobQuery<BianChengInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<BianChengInfo>() {
            @Override
            public void done(List<BianChengInfo> list, BmobException e) {
                if (e == null) {
                    Log.e("getBianChengInfo", "ok"+list.size());
                    tianKongs.addAll(list);
                    getWrongQuestion(account,type,resultCode,handler);
                } else {
                    Log.e("getBianChengInfo", e.getMessage());
                }
            }
        });
    }

    /**
     * 保存题
     * @param resultCode
     * @param handler
     */
    public void saveChooseInfo(final List<BianChengInfo> list, final int resultCode, final Handler handler){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<list.size();i++ ){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    final int finalI = i;
                    list.get(i).save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Log.e("saveChooseInfo", "ok"+"---"+ finalI);
                            } else {
                                Log.e("saveChooseInfo", e.getMessage());
                            }
                        }
                    });
                }
                handler.sendEmptyMessage(resultCode);
            }
        }).start();

    }

    /**
     * 获取知识点
     * @param knowlist
     * @param resultCode
     * @param handler
     */
    public void getKnowledge(final List<Knowledge> knowlist, final int resultCode, final Handler handler){
        BmobQuery<Knowledge> query = new BmobQuery<>();
        query.findObjects(new FindListener<Knowledge>() {
            @Override
            public void done(List<Knowledge> list, BmobException e) {
                if (e == null) {
                    Log.e("getKnowledge", "ok");
                    knowlist.clear();
                    knowlist.addAll(list);
                    handler.sendEmptyMessage(resultCode);
                } else {
                    Log.e("getKnowledge", e.getMessage());
                }
            }
        });
    }

}